<script>
var batailles = <?php echo $nb_batailles; ?>;
var camps = [0,<?php $i=0;foreach($camps as $c){
			if($i>0) echo ',';
			echo $c;
			$i++;
		} 
?>];
//A l'initialisation
$(function () {
	//Fonctions de click 
	$(".addc").click(function() {
		add_camp($(this));
	});

	$("[id^=btnLeft]").click(function () {
		var selectedItem = $("#rightValues"+$(this).attr('class')+" option:selected");
		$("#leftValues"+$(this).attr('class')).append(selectedItem);
		var text = "";
	    $("#rightValues"+$(this).attr('class')+" option").each(function(){
		if(text != "") text +=","
		text += $(this).val();
	    });
	    $("#txtRight"+$(this).attr('class')).val(text);
	});

	$("[id^=btnRight]").click(function () {
	    var selectedItem = $("#leftValues"+$(this).attr('class')+" option:selected");
	    $("#rightValues"+$(this).attr('class')).append(selectedItem);
	    var text = "";
	    $("#rightValues"+$(this).attr('class')+" option").each(function(){
		if(text != "") text +=","
		text += $(this).val();
	    });
	    $("#txtRight"+$(this).attr('class')).val(text);
	});
	
	$("[id^=supb]").unbind().click(function(e) {
		e.stopPropagation();
		supr_bataille($(this));
	});
	$("[id^=supc]").unbind().click(function(e) {
		e.stopPropagation();
		supr_camp($(this));
	});
});
//Ajoute un formulaire de camp
function add_camp(ent){
	var bat = ent.attr('id');
	camps[bat]++;
	var camp = camps[bat];
	var text = "";
	if(camp > 1) text += "<hr class=\"supc"+bat+'-'+camp+"\">";
	text += '<div id="div_camp'+bat+'-'+camp+'" class="supc'+bat+'-'+camp+'"><br>';
	text += '<label for="nom_camp'+bat+'-'+camp+'">Nom : </label>';
	text += '<input type="text" name="nom_camp'+bat+'-'+camp+'" id="nom_camp'+bat+'-'+camp+'"/\><br>';
	text += '<label for="wiki_camp'+bat+'-'+camp+'">Wiki : </label>';
	text += '<input type="url" name="wiki_camp'+bat+'-'+camp+'" id="wiki_camp'+bat+'-'+camp+'"/\><br>';
	text += '<label for="morts_camp'+bat+'-'+camp+'">Nombre de morts : </label>';
	text += '<input type="number" name="morts_camp'+bat+'-'+camp+'" id="morts_camp'+bat+'-'+camp+'" min="0" value="0"/\><br>';
	text += '<label for="blesses_camp'+bat+'-'+camp+'">Nombre blessés : </label>';
	text += '<input type="number" name="blesses_camp'+bat+'-'+camp+'" id="blesses_camp'+bat+'-'+camp+'" min="0" value="0"/\><br>';
	text += '<section class="container '+bat+'-'+camp+'"><div><select id="leftValues'+bat+'-'+camp+'" class="'+bat+'-'+camp+'" multiple>';
	<?php 
		foreach($entite as $e){
			echo "text += '<option value=\"".$e->get_identite()."\" >".$e->get_nomentite()."</option>';";
		}
	?>
	text += '</select></div><div>';
	text += '<input type="button" id="btnLeft'+bat+'-'+camp+'" class="'+bat+'-'+camp+'" value="&lt;&lt;" /\>';
	text += '<input type="button" id="btnRight'+bat+'-'+camp+'" class="'+bat+'-'+camp+'" value="&gt;&gt;" /\>';
	text += '</div><div><select id="rightValues'+bat+'-'+camp+'" class="'+bat+'-'+camp+'" multiple></select>';
	text += '<div><input type="text" id="txtRight'+bat+'-'+camp+'" name="addR'+bat+'-'+camp+'" style="display : none" /\></div></div></section>';
	text += '<input type="button" id="supc'+bat+'-'+camp+'" class="'+bat+'-'+camp+'" value="supprimer ce camp"/\></div>';
     	$("#"+bat).before(text);

	$("[id^=btnLeft]").click(function () {
		var selectedItem = $("#rightValues"+$(this).attr('class')+" option:selected");
		$("#leftValues"+$(this).attr('class')).append(selectedItem);
		var text = "";
	    	$("#rightValues"+$(this).attr('class')+" option").each(function(){
		if(text != "") text +=","
		text += $(this).val();
	    });
	    $("#txtRight"+$(this).attr('class')).val(text);
	});

	$("[id^=btnRight]").click(function () {
	    var selectedItem = $("#leftValues"+$(this).attr('class')+" option:selected");
	    $("#rightValues"+$(this).attr('class')).append(selectedItem);
	    var text = "";
	    $("#rightValues"+$(this).attr('class')+" option").each(function(){
		if(text != "") text +=","
		text += $(this).val();
	    });
	    $("#txtRight"+$(this).attr('class')).val(text);
	});

	$("[id^=supc]").unbind().click(function(e) {
		e.stopPropagation();
		supr_camp($(this));
	});

}

//Supprime une bataille
function supr_bataille(bat){
	batailles--;
	var id = $("[name=id_bataille"+bat.attr("class")+"]").attr("value");
	$.get( "http://codd.u-strasbg.fr/~pneumann/bdd/student-project-historicaldatabase/?section=conflit&action=delete_bataille&id="+id, function( data ) {
	});
	$("."+bat.attr("id")).remove();
}

//Supprime un camp
function supr_camp(bat){
	camps[bat.attr("class").substring(0, bat.attr("class").indexOf('-'))]--;
	var id = $("[name=id_camp"+bat.attr("class")+"]").attr("value");
	$.get( "http://codd.u-strasbg.fr/~pneumann/bdd/student-project-historicaldatabase/?section=conflit&action=delete_camp&id="+id, function( data ) {
	});
	$("."+bat.attr("id")).remove();
}

//Ajoute un formulaire de bataille
function add_batailles_form(){
	batailles++;
	var text = "";
	if(batailles > 1) text += "<hr class=\"supb"+batailles+"\">";
	text += '<div id="div_bataille'+batailles+'" class="supb'+batailles+'"><br>';
	text += '<label for="nom_bataille'+batailles+'">Nom : </label>';
	text += '<input type="text" name="nom_bataille'+batailles+'" id="nom_bataille'+batailles+'"/\><br>';
	text += '<label for="wiki_bataille'+batailles+'">Wiki : </label>';
	text += '<input type="url" name="wiki_bataille'+batailles+'" id="wiki_bataille'+batailles+'"/\><br>';
	text += '<label for="debut_bataille'+batailles+'">Date de création (jj/mm/saaaa): </label>';
	text += '<input type="text" name="debut_bataille'+batailles+'" id="debut_bataille'+batailles+'"/\><br>'
	text += '<label for="fin_bataille'+batailles+'">Date de dissolution (jj/mm/saaaa): </label>';
	text += '<input type="text" name="fin_bataille'+batailles+'" id="fin_bataille'+batailles+'"/\><br><br>';
	text += '<fieldset id="field'+batailles+'"><legend>Camps</legend><input id="'+batailles+'" class="addc" type="button" value="Ajouter un camp"/\></fieldset><br>';
	text += '<input type="button" id="supb'+batailles+'" class="'+batailles+'" value="supprimer cette bataille"/\></div>';
     	$("#add").before(text);
	camps[batailles] = 0;
	$(".addc").click(function() {
		add_camp($(this));
	});
	$("[id^=supb]").unbind().click(function(e) {
		e.stopPropagation();
		supr_bataille($(this));
	});
}

</script>

